home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / PrintfGWorld.c < prev    next >
Text File  |  1995-08-13  |  3KB  |  105 lines

  1. /*
  2. PrintfGWorld.c
  3.  
  4.     void PrintfGWorld(GWorldPtr our);
  5. Uses "printf" to print out the GWorld as a bitmap, representing blank as '.' and
  6. non-blank as '#'. The origin is designated '+'. Uses the upper lefthand pixel
  7. as the definition of blank (a poor but convenient assumption). This is very crude, 
  8. and intended only for debugging.
  9.  
  10.     void PrintStringAsBitmap(unsigned char *s);
  11. Uses DrawString to render the string in a GWorld, and then calls PrintfGWorld to
  12. render the GWorld onto the console. For debugging.
  13.  
  14. HISTORY:
  15. 1/17/94 dgp wrote it.
  16. 6/18/94 dgp Call SetGWorld(GetMainDevice())) before calling printf.
  17. 6/22/94 dgp Fixed bug: I was "restoring" to a GWorldPtr that was never initialized.
  18. 9/5/94 dgp removed assumption in printf's that int==short.
  19. */
  20. #include "VideoToolbox.h"
  21. void PrintfGWorld(GWorldPtr our);
  22. void PrintStringAsBitmap(unsigned char *s);
  23.  
  24. void PrintfGWorld(GWorldPtr world)
  25. // Print GWorld on the console as a bitmap.
  26. // Assume upper lefthand pixel is blank, which is rendered as '.'. 
  27. // Any other pixel value is represented as '#'.
  28. {
  29.     Rect r;
  30.     register unsigned long *pix,blank;
  31.     register int i,n;
  32.     int y;
  33.     char *bit;
  34.     GDHandle oldDevice;
  35.     long qD=0;
  36.   
  37.     Gestalt(gestaltQuickdrawVersion,&qD);
  38.     if(qD>=gestalt8BitQD){
  39.         oldDevice = GetGDevice();
  40.         SetGDevice(GetMainDevice());    // needed for printf
  41.     }
  42.     r=world->portRect;
  43.     r.left=0;
  44.     #if (THINK_C || THINK_CPLUS || SYMANTEC_C)
  45.         r.right=console_options.ncols;
  46.     #elif __MWERKS__
  47.         r.right=SIOUXSettings.columns;
  48.     #else
  49.         r.right=72;
  50.     #endif
  51.     CenterRectInRect(&r,&world->portRect);
  52.     SectRect(&r,&world->portRect,&r);
  53.     n=r.right-r.left;
  54.     bit=(char *)NewPtr((1+n)*sizeof(char));
  55.     if(bit==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n",(long)n+1);
  56.     bit[n]=0;
  57.     pix=(unsigned long *)NewPtr(n*sizeof(unsigned long));
  58.     if(pix==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n"
  59.         ,n*sizeof(long));
  60.     GetWindowPixelsQuickly((WindowPtr)world,world->portRect.left,world->portRect.top,pix,1);
  61.     blank=pix[0];
  62.     for(y=r.top;y<r.bottom;y++){
  63.         GetWindowPixelsQuickly((WindowPtr)world,r.left,y,pix,n);
  64.         for(i=0;i<n;i++){
  65.             if(pix[i]!=blank) bit[i]='#';                    // non-blank
  66.             else bit[i]='.';                                // blank
  67.         }
  68.         if(y==0 && -r.left>=0 && -r.left<n) bit[-r.left]='+';    // the origin
  69.         printf("%s\n",bit);
  70.     }
  71.     DisposePtr((void *)pix);
  72.     DisposePtr((void *)bit);
  73.     if(qD>=gestalt8BitQD)SetGDevice(oldDevice);
  74. }
  75.  
  76. void PrintStringAsBitmap(unsigned char *s)
  77. {
  78.     GWorldPtr world,old;
  79.     GDHandle oldDevice;
  80.     FontInfo f;
  81.     Rect r;
  82.     int error;
  83.  
  84.     // Draw string into a new GWorld
  85.     GetFontInfo(&f);
  86.     SetRect(&r,0,-f.ascent,StringWidth(s),f.descent);    // nominal size
  87.     InsetRect(&r,-(f.widMax/2+2),-(f.leading+2));        // add margin
  88.     error=NewGWorld(&world,1,&r,NULL,NULL,0);
  89.     if(error)PrintfExit("PrintStringAsBitmap: NewGWorld error %d.\n",error);
  90.     GetGWorld(&old,&oldDevice);
  91.     SetGWorld(world,NULL);
  92.     TextFace(old->txFace);
  93.     TextFont(old->txFont);
  94.     TextSize(old->txSize);
  95.     EraseRect(&world->portRect);
  96.     MoveTo(0,0);
  97.     DrawString(s);
  98.     SetGWorld(old,oldDevice);
  99.     
  100.     PrintfGWorld(world);
  101.     
  102.     DisposeGWorld(world);
  103. }
  104.  
  105.